#!/usr/bin/env python

import optparse
import os
try:
    from logging import logger
    log = logger.constructLog(appName='deploy')
except ImportError:
    class TinyLog:
        def __printString(self, level, message):
            print '{level}: {message}'.format(level=level, message=message)
        def info(self, message):
            self.__printString('INFO', message)
        def fatal(self, message):
            self.__printString('FATAL', message)
        def debug(self, message):
            self.__printString('DEBUG', message)
        def error(self, message):
            self.__printString('ERROR', message)
    log = TinyLog()


class FolderValidator:
    class InvalidFolder(Exception): fatal = True

    @staticmethod
    def validateFolder(path):
        if not (os.path.exists(path) and os.path.isfile(os.path.join(path, 'app.yaml'))):
            raise FolderValidator.InvalidFolder('No Google App Engine application detected.'
                                                ' Path: {0}'.format(path))


def main():
    argParser = optparse.OptionParser(description='deploy is a tool for deploying server app to google server')
    argParser.add_option('-v', '--verbose',
                         action='store_true', dest='verbose', default=False,
                         help='noise level of logging')
    argParser.add_option('-f', '--app-folder',
                         action='store', dest='appFolder', default='CWD', type='string',
                         metavar='DIR',
                         help='path to application folder')
    options, args = argParser.parse_args()
    if options.appFolder == 'CWD':
        options.appFolder = os.getcwd()
    log.info('start deploying')
    try:
        FolderValidator.validateFolder(options.appFolder)
    except FolderValidator.InvalidFolder as ex:
        if hasattr(ex, 'fatal'):
            log.fatal(ex.message)
        else:
            log.error(ex.message)


if __name__ == '__main__':
    main()
  